<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8"/>
	<title>添加信息</title>

	<link rel="stylesheet" href="../layui/css/layui.css">
	<script type="text/javascript" src="../layui/layui.js"></script>
	<script type="text/javascript" src="../js/jquery-1.12.2.min.js"></script>
</head>
	<body>
	<form class="layui-form" action="" lay-filter="example">
		<div class="layui-form-item">
			<label class="layui-form-label">项目名称</label>
			<div class="layui-input-block">
				<input type="text" style="border: none" name="projectName" lay-verify="required" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">前缀</label>
			<div class="layui-input-block">
				<input type="text" style="border: none" name="prefix" lay-verify="required" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-inline">
				<label class="layui-form-label">时间</label>
				<div class="layui-input-block">
					<select name="timeFormat" id="timeFormat">
						<option value="">请选择时间</option>
					</select>
				</div>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-inline">
				<label class="layui-form-label">流水号</label>
				<div class="layui-input-block">
					<select name="serialNum" id="serialNum">
						<option value="">请选择流水号</option>
					</select>
				</div>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">编号预览</label>
			<div class="layui-input-block">
				<input type="text" style="border: none" name="numberPreview" lay-verify="required" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
		<script>
            //一般直接写在一个js文件中
            layui.use(['layer', 'form'], function(){
                var layer = layui.layer
                    ,form = layui.form;

                // //自定义验证规则
                // form.verify({
                //     phone: [
                //         /^1[35678]\d{9}$/
                //         ,'手机格式不对'
                //     ]
                //     ,age:[/^\d{1,3}$/, '年龄格式不对' ]
                // });


                //监听提交
                form.on('submit(demo1)', function(data){
//                    layer.alert(JSON.stringify(data.field), {
//                        title: '最终的提交信息'
//                    })
                    $.ajax({
                        type:"post",
                        url:"/number/update" + window.location.search,
                        data:data.field, // 获取提交的数据
                        dataType:"json",
                        success:function(data){
                            if(data.code == 0){
                                var t = parent.layui.table;
                                t.reload('demo');// 重新加载页面,lay-filter="test"
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            }else{
                                alert(data.info);
                            }
                        }
                    })
                    // 阻止默认的提交
                    return false;
                });
				$.ajax({
					type: "get",
					url: "/number/serialNum",
					async: false,
					success: function (res) {
						if (res.code == 0) {
							$(res.data).each(function () {
								console.log(this.serialNum)
								var html = "<option value='" + this.id + "'>" + this.serialNum + "</option>"
								$("#serialNum").append($(html));
							})
							//重新渲染
							layui.form.render("select");
						} else {
							layer.msg(res.message);
						}
					}
				});
				$.ajax({
					type: "get",
					url: "/number/timeFormat",
					async: false,
					success: function (res) {
						if (res.code == 0) {
							$(res.data).each(function () {
								console.log(this.type)
								var html = "<option value='" + this.id + "'>" + this.type + "</option>"
								$("#timeFormat").append($(html));
							})
							//重新渲染
							layui.form.render("select");
						} else {
							layer.msg(res.message);
						}
					}
				});
                $.ajax({
					type:"get",
					url:"/number/find" + window.location.search,
					dataType:"json",
					success:function(res){
						console.log(res)
					    if(res.code == 0){
                            //表单初始赋值
							// 根据输入控件的name的值进行赋值操作
                            form.val('example',res.data);
						}
					}
				})
                //layer.msg('Hello World');
            });
		</script>
	</body>

</html>